﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace buddyreset
{
    public partial class MainForm : Form
    {
        string _settingsFileName = Path.Combine(Application.StartupPath, "settings.xml");

        public MainForm()
        {
            InitializeComponent();
        }

        void DisplayInstances(string path)
        {
            textBoxPath.Text = path;

            listBoxInstances.Items.Clear();

            foreach (string dir in Directory.GetDirectories(path, "wow_*"))
            {
                listBoxInstances.Items.Add(Path.GetFileNameWithoutExtension(dir));
            }
        }

        private void buttonPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog d = new FolderBrowserDialog();
            if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DisplayInstances(d.SelectedPath);
                Properties.Settings.Default.Path = d.SelectedPath;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.Path))
            {
                DisplayInstances(Properties.Settings.Default.Path);
                Enable(false);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        void ClearLogs(string instanceName)
        {
            string dir = Path.Combine(Properties.Settings.Default.Path, instanceName, "buddy\\Logs");
            ClearDir(dir);
        }

        void ClearCache(string instanceName)
        {
            string dir = Path.Combine(Properties.Settings.Default.Path, instanceName, "buddy\\Cache");
            ClearDir(dir);
        }

        void ClearAssemblies(string instanceName)
        {
            string dir = Path.Combine(Properties.Settings.Default.Path, instanceName, "buddy\\CompiledAssemblies");
            ClearDir(dir);
        }

        void ClearDir(string dir)
        {
            StringBuilder sb = new StringBuilder();

            ClearRecursive(dir, sb);

            if (sb.Length > 0)
            {
                MessageBox.Show(
                    sb.ToString(),
                    "Exception",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);
            }
        }

        void ClearRecursive(string dir, StringBuilder sb)
        {
            foreach (string subdir in Directory.GetDirectories(dir))
            {
                ClearRecursive(subdir, sb);
                Directory.Delete(subdir);
            }

            foreach (string file in Directory.GetFiles(dir))
            {
                try
                {
                    File.Delete(file);
                }
                catch (Exception ex)
                {
                    sb.AppendLine(file + ": " + ex.Message);
                }
            }
        }

        private void buttonClearLog_Click(object sender, EventArgs e)
        {
            ClearLogs((string)listBoxInstances.SelectedItem);
        }

        private void buttonClearCache_Click(object sender, EventArgs e)
        {
            ClearCache((string)listBoxInstances.SelectedItem);
        }

        private void buttonClearAssemblies_Click(object sender, EventArgs e)
        {
            ClearAssemblies((string)listBoxInstances.SelectedItem);
        }

        private void buttonClearAll_Click(object sender, EventArgs e)
        {
            string instance = (string)listBoxInstances.SelectedItem;
            ClearLogs(instance);
            ClearCache(instance);
            ClearAssemblies(instance);
        }

        void Enable(bool enable)
        {
            buttonClearAll.Enabled = enable;
            buttonClearLog.Enabled = enable;
            buttonClearCache.Enabled = enable;
            buttonClearAssemblies.Enabled = enable;
        }

        private void listBoxInstances_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enable = listBoxInstances.SelectedIndex >= 0;
            Enable(enable);
        }
    }
}
